<script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.min.js" charset="UTF-8"></script>
 <script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.easyui.min.js" charset="UTF-8"></script>
 <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css"></link>
 <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/icon.css" type="text/css"></link>
 <link rel="stylesheet" href="../css/main.css" type="text/css"></link>
 <script type="text/javascript" src="../js/mine.js"></script>
 <script type="text/javascript" src="../js/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js" charset="UTF-8"></script>
 <script type="text/javascript" src="../js/validate.js" charset="UTF-8"></script>  
<script type="text/javascript" charset="UTF-8">
	var studentDatagrid;
	var adminStudentLayout;
	var adminStudentForm;
	var editRow;	
	var sexData = [{
		"id":"male",
		"text":"男"
	},{
		"id":"female",
		"text":"女",
	}];
	
	var stypeData = [{
		"id":"美工",
		"text":"美工"
	},{
		"id":"前端",
		"text":"前端",
	},{
		"id":"后台",
		"text":"后台"
	}];
	
	$(function(){
		editRow = undefined;
		adminStudentForm = $('#adminStudentForm').form();
		adminStudentLayout = $('#adminStudentLayout').layout();
		studentDatagrid = $('#studentDatagrid').datagrid({
			url:'../servlet/GetStudentServlet',
			//title:'学生信息',
			//iconCls:'icon-save',
			fitColumns:true,
			nowrap:false,
			striped:true,
			idField:'sno',
			pagination:true,
			pageSize:10,
			pageList:[10,20,30],
			fit:true,
			border:false,
			sortName:'sno',
			sortOrder:'desc',
			columns:[[{
				field:'cut',
				checkbox:true
			},{
				title:'学号',
				field:'sno',
				width:100,
				sortable:true,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						validType:'nums'			
					}
				}
			},{
				title:'姓名',
				field:'sname',
				width:100,
				sortable:true,
				editor:{
					type:'validatebox',
					options:{
						required:true,
					}
				}
			},{
				title:'性别',
				field:'sex',
				width:100,
				sortable:true,
				editor:{
					type:'combobox',
					options:{
						required:true,
						data:sexData,
						valueField:'id',
						textField:'text',
						editable:false
					}
				},
				formatter:function(value,rowData,rowIndex){
					 for(var i=0; i<sexData.length; i++){  
			                if (sexData[i].id == value) {
			                	return sexData[i].text;  
			                }
		            }  
				}
			},{
				title:'专业',
				field:'major',
				width:100,
				editor:{
					type:'validatebox',
					options:{
						required:true
					}
				}
			},{
				title:'年级',
				field:'grade',
				width:100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						validType:'grade[4]'
					}
				}
			},{
				title:'分工',
				field:'stype',
				width:100,
				editor:{
					type:'combobox',
					options:{
						required:true,
						//mode:'remote',
						//url:'../servlet/GetStypeServlet',
						data:stypeData,
						valueField:'id',
						textField:'text',
						delay:1000
					}
				}
			},{
				title:'参与项目数',
				field:'proNum',
				width:100
			}]],
			toolbar:[{
				text:'增加',
				iconCls:'icon-add',
				handler:function(){
					addStudent();
				}
			},'-',{
				text:'删除',
				iconCls:'icon-cancel',
				handler:function(){
					deleteStudent();
				}
			},'-',{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
					modifyStudent();
				}
			},'-',{
				text:'重置密码',
				iconCls:'icon-redo',
				handler:function(){
					resetPsw();
				}
			},'-',{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){
					studentDatagrid.datagrid('endEdit', editRow);
				}
			},'-',{
				text:'取消',
				iconCls:'icon-undo',
				handler:function(){
					editRow = undefined;
					studentDatagrid.datagrid('unselectAll');
					studentDatagrid.datagrid('rejectChanges');
				}
			},'-',{
				text:'刷新',
				iconCls:'icon-reload',
				handler:function(){
					studentDatagrid.datagrid('unselectAll');
					studentDatagrid.datagrid('rejectChanges');
					studentDatagrid.datagrid('load');
				}
			},'-'],
			onAfterEdit:function(rowIndex, rowData, changes) {
				console.info(rowData);
				var inserted = studentDatagrid.datagrid('getChanges','inserted');
				console.info(inserted);
				var updated = studentDatagrid.datagrid('getChanges','updated');
				console.info(updated);
				
				if(inserted.length > 0 || updated.length > 0) {
					var url = '';
					if(inserted.length > 0) {
						url = '../servlet/InsertStudentServlet';
					} else if(updated.length > 0) {
						console.info(rowData.stype);
						url = '../servlet/UpdateStudentServlet';
					}
					
					$.ajax({
						url:url,
						data:rowData,
						type:'post',
						dataType:'json',
						success:function(r) {
							if(r && r.success) {
								studentDatagrid.datagrid('acceptChanges');
								studentDatagrid.datagrid('reload');
								$.messager.show({
									title:'提示',
									msg:r.msg,
								});
							} else {
								studentDatagrid.datagrid('rejectChanges');
								$.messager.alert('出错啦',r.msg,'error');
							}
						}
					});
					
				} 
				
				editRow = undefined;
			},
			onRowContextMenu:function(e, rowIndex, rowData) {
				e.preventDefault();
				//$(this).datagrid('unselectAll');
				//$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left:e.pageX,
					top:e.pageY
				});
			}
		
		});
		
		$('#stype').combobox({
			data:[{
				"id":"all",
				"text":"全部",
			},{
				"id":"美工",
				"text":"美工"
			},{
				"id":"前端",
				"text":"前端",
			},{
				"id":"后台",
				"text":"后台"
			}],
			valueField:'id',
			textField:'text',
			delay:1000
		});
		
		$('#sex').combobox({
			data:[{
				"id":"all",
				"text":"全部",
			},{
				"id":"male",
				"text":"男"
			},{
				"id":"female",
				"text":"女",
			}],
			valueField:'id',
			textField:'text',
			editable:false
		});
		
	});
	
	addStudent = function() {
		if(editRow == undefined) {
			studentDatagrid.datagrid('unselectAll');
			studentDatagrid.datagrid('addEditor',[{
				field:'sno',
					editor:{
						type:'validatebox',
					    options:{
						   required:true,
						   validType:'nums'			
					    }
					}
			},{
				field:'sex',
				editor:{
					type:'combobox',
					options:{
						required:true,
						data:sexData,
						valueField:'id',
						textField:'text',
						editable:false
					}
				}
			}]); 
			 
			studentDatagrid.datagrid('insertRow', {
				index:0,
				row:{
					sno:'',
					sname:'',
					sex:'',
					major:'',
					grade:'',
					stype:''
				}
			});
			
			studentDatagrid.datagrid('selectRow', 0);
			studentDatagrid.datagrid('beginEdit', 0);
			editRow = 0;
		} else {
			studentDatagrid.datagrid('endEdit',editRow);
		}
	}
	
	deleteStudent = function() {
		var rows = studentDatagrid.datagrid('getSelections');
		var ids = [];
		var cantd = [];
		var m = '';
		var cantm = '';
		
		if(rows.length > 0) {
			for(var i=0;i<rows.length; i++) {
				if(rows[i].proNum == '0' && rows[i].sno != '') {
					ids.push(rows[i].sno);
				} else {
					cantd.push(rows[i].sno);
					cantm += "<br/> [" + rows[i].sno + "&nbsp;&nbsp;&nbsp;&nbsp;" + rows[i].sname + "]";
				}
			}
			
			if(cantd.length > 0 && ids.length == 0) {//选择的数据都不能删除
				m = '选择的数据都不能删除。';
				$.messager.alert('提示',m,'warning');
			} else {
				if(cantd.length > 0 && ids.length > 0){//选择的数据部分不能删除
					m = '<div style="width:10px;margin:auto auto auto 45px;">选择的数据中:' + cantm + '<br/>还有项目信息,不能删除.<br>确定要删除选中的其余数据吗?</div>';
				} else if(cantd.length == 0 && ids.length > 0) {//选择的数据都可以删除
					m = '确定要删除所选择的学生吗?';
				}
			
				$.messager.confirm('提示',m,function(b) {
					if(b) {
						$.ajax({
							url:'../servlet/DeletStudentServlet',
							data:{
								ids:ids.join(',')//用,分隔
							},
							type:'post',
							dataType:'json',
							success:function(r) {
								if(r && r.success) {
									studentDatagrid.datagrid('acceptChanges');
									studentDatagrid.datagrid('load');
									studentDatagrid.datagrid('unselectAll');
									studentDatagrid.datagrid('rejectChanges');
									//console.info(studentDatagrid.datagrid('getSelections'));
									$.messager.show({
										title:'提示',
										msg:r.msg
									});
								} else {
									$.messager.alert('提示',r.msg,'error');
								}
							}
						});
					}
				});
			}
		} else {
			$.messager.alert('提示','未选择任何数据.','warning');
		}	
	}
		
	modifyStudent = function() {
		var rows = studentDatagrid.datagrid('getSelections');
		if(rows.length == 1 ) {
			studentDatagrid.datagrid('removeEditor', ['sno','sex']);//去除sno,sex的编辑器
			
			if(editRow == undefined) {
				var index = studentDatagrid.datagrid('getRowIndex', rows[0]);
				if(rows[0].sno != '') {
					studentDatagrid.datagrid('beginEdit', index);
					editRow = index;
					//studentDatagrid.datagrid('unselectAll');
				} else {
					$.messager.alert('提示','空白数据不能进行修改.', 'warning');
				}
			} else {
				studentDatagrid.datagrid('unselectAll');
				studentDatagrid.datagrid('endEdit', editRow);
			}
		} else if(rows.length > 1) {
			$.messager.alert('提示','只能选择一行数据进行修改.','warning');
		} else {
			$.messager.alert('提示','未选择需要修改的数据.', 'warning');
		}
	}
	
	resetPsw = function() {
		var rows = studentDatagrid.datagrid('getSelections');
		var ids = [];
		if(rows.length > 0) {
			for(var i=0;i<rows.length; i++) {
				ids.push(rows[i].sno);
			}
			if(ids[0] != '') {
				$.messager.confirm('提示','你确定要重置选中学生的密码吗?',function(b) {
					if(b) {
						$.ajax({
							url:'../servlet/ResetPswServlet',
							data:{
								ids:ids.join(','),    //用,分隔
								userRole:'student'
							},
							type:'post',
							dataType:'json',
							success:function(r) {
								if(r && r.success) {
									studentDatagrid.datagrid('acceptChanges');
									studentDatagrid.datagrid('load');
									studentDatagrid.datagrid('unselectAll');
									studentDatagrid.datagrid('rejectChanges');
									//console.info(studentDatagrid.datagrid('getSelections'));
									$.messager.show({
										title:'提示',
										msg:r.msg
									});
								} else {
									$.messager.alert('提示',r.msg,'error');
								}
							}
						});
					}
				});
			} else {
				$.messager.alert('提示','空白数据不能进行操作.','warning');
			}
		} else {
			$.messager.alert('提示','未选择任何数据.','warning');
		}
	}
	
	searchStudent = function() {
		//console.info(serializeObject(adminStudentForm));
		studentDatagrid.datagrid('load',serializeObject(adminStudentForm));
	}
	
	cleanStudent = function() {
		studentDatagrid.datagrid('load',{});
		adminStudentForm.find('input').val('');
	}
</script>

<div id="adminStudentLayout" fit="true" border="false" >
		<div region="north" border="false" split="false" title="查询" style="margin:0 auto;height:81px;" >
			<form id="adminStudentForm" class="datagrid-toolbar"  style="margin:0 auto;">
				<table>
					<tr>
						<td><b>学号</b><input type="text" name="sno" class="easyui-validatebox"  placeholder="您只能输入数字" data-options="validType:'nums'"  /></td>
						<td><b>姓名</b><input type="text" name="sname"/></td>
						<td><b>性别</b><input type="text" name="sex" id="sex"/></td>
					</tr>
					<tr>
						<td><b>专业</b><input type="text" name="major" /></td>
						<td><b>年级</b><input type="text" name="grade" class="easyui-validatebox" placeholder="您只能输入4位数字" data-options="validType:'grade[4]'"  /></td>
						<td>
							<b>分工</b><input type="text" name="stype" id="stype" />
						</td>
						<td>
							<a href="javascript:void(0);" class="easyui-linkbutton"  iconCls="icon-search" pain="false" onclick="searchStudent();" ></a>
						</td>
						<td>	
							<a href="javascript:void(0);" class="easyui-linkbutton"  pain="false" onclick="cleanStudent();" >清空</a>
						</td>
						
					</tr>
				</table>
				</fieldset>
			</form>
	    </div>
	    <div region="center" border="false" >
			<table id="studentDatagrid"></table>
		</div>
			
		<div id="menu" class="easyui-menu" style="width:120px;display:none;">
			<div onclick="addStudent();" iconCls="icon-add" >增加</div>
			<div onclick="deleteStudent();" iconCls="icon-remove" >删除</div>
			<div onclick="modifyStudent();" iconCls="icon-edit" >修改</div>
			<div onclick="resetPsw();" iconCls="icon-edit" >重置密码</div>
		</div>
</div>

	
